package org.aphelie.chronicle.ui.forms;

import java.text.MessageFormat;

import org.aphelie.chronicle.ArcTopic;
import org.aphelie.chronicle.ui.ChronicleUIPlugin;
import org.aphelie.chronicle.ui.editors.ArcFormEditor;
import org.aphelie.chronicle.ui.editors.ArcFormPage;
import org.aphelie.chronicle.ui.i18n.ChronicleUIMessages;
import org.aphelie.chronicle.ui.layout.ArcFormLayoutFactory;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.TableWrapData;

public class ArcTopicPage extends ArcFormPage {
	
	private ArcTopic topic;
	
	protected IManagedForm managedForm;
	
	public ArcTopicPage(ArcFormEditor editor, ArcTopic topic) {
		super(editor, getPageId(topic), getPageTitle(topic));
		this.topic = topic;
	}
	
	protected final ArcTopic getTopic() {
		return topic;
	}
	
	FormToolkit getToolkit() {
		return managedForm.getToolkit();
	}
	
	protected static String getPageId(ArcTopic topic) {
		return MessageFormat.format(ChronicleUIMessages.ArcTopic_Page_Id, topic.getLabel());
	}
	
	protected static String getPageTitle(ArcTopic topic) {
		return MessageFormat.format(ChronicleUIMessages.ArcTopic_Page_Title, topic.getLabel());
	}
	
	@Override
	protected void createFormContent(IManagedForm managedForm) {
		super.createFormContent(managedForm);
		this.managedForm = managedForm;
        fillBody();
	}
	
	protected void fillBody() {
		Composite body = managedForm.getForm().getBody();
		body.setLayout(ArcFormLayoutFactory.createFormPaneTableWrapLayout(true, 2));
		ChronicleUIPlugin.getDefault().setCompositeBackgroundColor(body);
		fillLeft(body);
		fillRight(body);
	}
	
	protected void fillLeft(Composite parent) {
		Composite left = getToolkit().createComposite(parent);
		left.setLayout(ArcFormLayoutFactory.createFormTableWrapLayout(true, 3));
		left.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB));
	}
	
	protected void fillRight(Composite parent) {
		Composite right = getToolkit().createComposite(parent);
		//right.setLayout(ArcFormLayoutFactory.createFormTableWrapLayout(true, 1));
		//right.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB));
		//fillMembers(right);
		ArcSectionFactory.fillBody(this, right, topic);
	}
	
/*	private void fillMembers(Composite parent) {
		Composite control = getToolkit().createComposite(parent);
		control.setLayout(ArcFormLayoutFactory.createFormTableWrapLayout(true, 8));
		control.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB));
		ChronicleUIPlugin.setCompositeBackgroundColor(control);
		
		if (topic.eContents().isEmpty()) {
			ChronicleShaperPlugin.shape(topic);
		}
		ArcTopicSection section;
		switch (topic.getGrade()) {
		case 0:

			break;
		case 1:
			section = ArcSectionFactory.createSection(this, parent, topic);
			managedForm.addPart(section);
		default:
			control.setLayout(ArcFormLayoutFactory.createSectionClientGridLayout(true, topic.eContents().size()));
			control.redraw();
			for(EObject content : topic.eContents()) {
				if (content instanceof ArcItem) {
					
				} else if (content instanceof ArcTopic){
					section = ArcSectionFactory.createSection(this, parent, (ArcTopic) content);
					managedForm.addPart(section);
				}
			}
			break;
		}

	}*/
	
}
